// pages/operation/student/courseDetails/index.js
import { StudentModel } from "../../../../api/api-work/student.js";
import { timestampToTime } from "../../../../utils/index.js"
const studentModel = new StudentModel()
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    query: {}, // 前面传递过来的对象
    selectRow: {},
    showExpireDate: false,
    expireDate: "",
    expireDateShow: new Date().getTime(),
    minDate: new Date(2020, 0, 1).getTime(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const params = JSON.parse(options.params);
    this.setData({
      query: params
    })
    console.log("购课详情：", this.data.query)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  // 设置有效期
  updateExpireDate(e) {
    const row = e.currentTarget.dataset.query;
    this.setData({
      selectRow: row,
      showExpireDate: true
    })
  },
  selectExpireDate(e) {
    this.setData({
      expireDate: timestampToTime(e.detail),
      expireDateShow: e.detail,
      showExpireDate: false
    })
    studentModel.studentCourseUpdateApi({ id: this.data.selectRow.id, expireDate: this.data.expireDate }).then(res => {
      let arr = this.data.query.tstudentCourseDtos;
      arr.forEach(item => {
        if (item.id == this.data.selectRow.id) {
          item.expireDate = this.data.expireDate
        }
      })
      this.setData({
        'query.tstudentCourseDtos': arr
      })
    })
  },
  onCloseExpireDate() {
    this.setData({
      showExpireDate: false
    })
  },
  // 课时清零
  clearCourseHour(e) {
    const row = e.currentTarget.dataset.query;
    Dialog.confirm({
      title: "确认课时清零？"
    }).then(() => {
      studentModel.studentCourseUpdateApi({ id: row.id, exhausted: '0' }).then(res => {
        let arr = this.data.query.tstudentCourseDtos;
        arr.forEach(item => {
          if (item.id == row.id) {
            item.exhausted = '0'
          }
        })
        this.setData({
          'query.tstudentCourseDtos': arr
        })
      })
    })
  },
  // 处理超上
  dealExceed(e) {
    const row = e.currentTarget.dataset.query;
    Dialog.confirm({
      title: "确认超上处理？"
    }).then(() => {
      studentModel.studentCourseDealExceedApi({ id: row.id }).then(res => {
        wx.navigateBack()
      })
    })
  },
  // 订单详情
  navigateToOrderDetails(e) {
    const params = JSON.stringify(e.currentTarget.dataset.query);
    wx.navigateTo({
      url: `/pages/operation/student/orderDetails/index?params=${params}`,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})